package com.example.demo;

import com.example.demo.entity.Building;
import com.example.demo.entity.EduSystem;
import com.example.demo.entity.User;
import com.example.demo.mapper.BuildingMapper;
import com.example.demo.mapper.EduSystemMapper;
import com.example.demo.mapper.UserMapper;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@ServletComponentScan

public class Demo1Application extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Demo1Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
    }

    @Bean
    public CommandLineRunner dataLoader(
            UserMapper userMapper,
            EduSystemMapper eduSystemMapper,
            BuildingMapper buildingMapper
    ) {
        return args -> {
            // Create an administrator if it doesn't exist.
            if (userMapper.findUserByID("1001") == null) {
                User admin = new User(
                        "1001",
                        "",
                        "admin",
                        "admin",
                        "",
                        "",
                        "admin",
                        1,
                        "",
                        "",
                        "",
                        "在岗",
                        null
                );
                userMapper.save(admin);
            }

            if(!eduSystemMapper.existsById("xk")){
                EduSystem eduSystem = new EduSystem(
                        "xk",
                        false
                );
                eduSystemMapper.save(eduSystem);
            }

            if(buildingMapper.findAll().size()==0){
                List<Building> buildingList = new ArrayList<>();
                Building building1 = new Building("第二教学楼");
                Building building2 = new Building("第三教学楼");
                Building building3 = new Building("第四教学楼");
                buildingList.add(building1);
                buildingList.add(building2);
                buildingList.add(building3);
                buildingMapper.saveAll(buildingList);
            }


        };
    }


}

